package com.rabbiter.oes.controller;

import com.rabbiter.oes.entity.Banks;
import com.rabbiter.oes.service.BanksService;
import com.rabbiter.oes.util.ApiResultHandler;
import com.rabbiter.oes.entity.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/banks")
public class BanksController {

    @Autowired
    private BanksService banksService;

    /**
     * 获取所有题库（无参时）
     */
    @GetMapping
    public ApiResult getAllBanks() {
        try {
            List<Banks> banks = banksService.listAllBanks();
            System.out.println("所有题库: " + banks);
            return ApiResultHandler.success(banks);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResultHandler.buildApiResult(500, "服务器内部错误，请稍后重试", null);
        }
    }

    /**
     * 按 subject 模糊搜索题库（带 subject 参数时）
     */
    @GetMapping(params = "subject")
    public ApiResult getBanksBySubject(@RequestParam String subject) {
        try {
            List<Banks> banks = banksService.listBanksBySubject(subject);
            System.out.println("搜索题库 (subject=" + subject + "): " + banks);
            return ApiResultHandler.success(banks);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResultHandler.buildApiResult(500, "搜索失败：服务器内部错误", null);
        }
    }
}//package com.rabbiter.oes.controller;
//
//import com.rabbiter.oes.entity.Banks;
//import com.rabbiter.oes.service.BanksService;
//import com.rabbiter.oes.util.ApiResultHandler;
//import com.rabbiter.oes.entity.ApiResult;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.CrossOrigin;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.util.List;
//
///**
// * 题库管理控制器
// */
//@RestController
//@RequestMapping("/banks")
//@CrossOrigin(origins = "*") // 请根据实际前端域名调整跨域设置
//public class BanksController {
//
//    @Autowired
//    private BanksService banksService;
//
//    /**
//     * 获取所有题库列表
//     * GET /api/banks
//     *
//     * 返回示例：
//     * {
//     *   "code": 200,
//     *   "message": "请求成功",
//     *   "data": [
//     *     { "bankId": 1, "subject": "2025新警入警考试题库" },
//     *   ]
//     * }
//     */
//    @GetMapping
//    public ApiResult getAllBanks() {
//        try {
//            List<Banks> banks = banksService.listAllBanks();
//            System.out.println("banks:"+banks);
//            return ApiResultHandler.success(banks);
//        } catch (Exception e) {
//            e.printStackTrace();
//            // 可以封装成更具体的错误码
//            return ApiResultHandler.buildApiResult(500, "服务器内部错误，请稍后重试", null);
//        }
//    }
//
//    @GetMapping(params = "subject")
//    public ApiResult getBanksBySubject(String subject) {
//        try {
//            List<Banks> banks = banksService.listBanksBySubject(subject);
//            return ApiResultHandler.success(banks);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ApiResultHandler.buildApiResult(500, "服务器内部错误", null);
//        }
//    }
//}